package com.forum.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.forum.annotation.IgnoreAuth;
import com.forum.entity.MockexamEntity;
import com.forum.entity.view.MockexamView;
import com.forum.service.MockexamService;
import com.forum.utils.MPUtil;
import com.forum.utils.PageUtils;
import com.forum.utils.R;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张继成
 * @since 2023-12-31
 */
@RestController
@RequestMapping("/exampaper")
@Api(tags = "模拟考试控制")
public class MockexamController {
    @Autowired
    private MockexamService exampaperService;

    /**
     * 后端列表
     */
    @RequestMapping("/page")
    @SuppressWarnings("unchecked")
    public R page(@RequestParam Map<String, Object> params, MockexamEntity exampaper,
                  HttpServletRequest request){
        EntityWrapper<MockexamEntity> ew = new EntityWrapper<MockexamEntity>();

        PageUtils page = exampaperService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, exampaper), params), params));

        return R.ok().put("data", page);
    }

    /**
     * 前端列表
     */
    @IgnoreAuth
    @RequestMapping("/list")
    @SuppressWarnings("unchecked")
    public R list(@RequestParam Map<String, Object> params, MockexamEntity exampaper,
                  HttpServletRequest request){
        EntityWrapper<MockexamEntity> ew = new EntityWrapper<MockexamEntity>();

        PageUtils page = exampaperService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, exampaper), params), params));
        return R.ok().put("data", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/lists")
    @SuppressWarnings("unchecked")
    public R list(MockexamEntity exampaper){
        EntityWrapper<MockexamEntity> ew = new EntityWrapper<MockexamEntity>();
        ew.allEq(MPUtil.allEQMapPre( exampaper, "exampaper"));
        return R.ok().put("data", exampaperService.selectListView(ew));
    }

    /**
     * 查询
     */
    @RequestMapping("/query")
    @SuppressWarnings("unchecked")
    public R query(MockexamEntity exampaper){
        EntityWrapper< MockexamEntity> ew = new EntityWrapper< MockexamEntity>();
        ew.allEq(MPUtil.allEQMapPre( exampaper, "exampaper"));
        MockexamView exampaperView =  exampaperService.selectView(ew);
        return R.ok("查询模拟考试表成功").put("data", exampaperView);
    }

    /**
     * 后端详情
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        MockexamEntity exampaper = exampaperService.selectById(id);
        return R.ok().put("data", exampaper);
    }

    /**
     * 前端详情
     */
    @IgnoreAuth
    @RequestMapping("/detail/{id}")
    public R detail(@PathVariable("id") Long id){
        MockexamEntity exampaper = exampaperService.selectById(id);
        return R.ok().put("data", exampaper);
    }




    /**
     * 后端保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MockexamEntity exampaper, HttpServletRequest request){
        exampaper.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
        //ValidatorUtils.validateEntity(exampaper);
        exampaperService.insert(exampaper);
        return R.ok();
    }

    /**
     * 前端保存
     */
    @RequestMapping("/add")
    public R add(@RequestBody MockexamEntity exampaper, HttpServletRequest request){
        exampaper.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
        //ValidatorUtils.validateEntity(exampaper);
        exampaperService.insert(exampaper);
        return R.ok();
    }



    /**
     * 修改
     */
    @RequestMapping("/update")
    @Transactional
    public R update(@RequestBody MockexamEntity exampaper, HttpServletRequest request){
        //ValidatorUtils.validateEntity(exampaper);
        exampaperService.updateById(exampaper);//全部更新
        return R.ok();
    }




    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        exampaperService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }
}
